from locust import HttpLocust, TaskSet, task
import random


class UserBehavior(TaskSet):

    def on_start(self):
        self.login()

    def login(self):
        users = []
        for i in range(3,11):
           user = "tom"+str(i)+"@mail.com"
           users.append(user)

        user_mail = random.sample(users, 1)
        self.client.post("/auth/login", {"email":user_mail[0],"password":"123"})
        username = user_mail[0].split("@")[0]
        with self.client.get("/", catch_response=True) as response:
            if bytes(username, encoding = "utf8") not in response.content:
                response.failure("User login error")

    @task
    def send_pop(self):
        self.client.post("/", {"body":"new test pop!"})

    @task
    def send_comments(self):
        self.client.post("/post/1",{"body":"test Comments"})

class WebsiteUser(HttpLocust):
    task_set = UserBehavior
    min_wait = 1000
    max_wait = 2000